package home.sockets.controller.parsers;

import home.sockets.controller.proxy.ProxyServer;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: Evgeny
 * Date: 13.01.13
 * Time: 1:36
 * To change this template use File | Settings | File Templates.
 */
public class Sock5Parser implements IPParser
{
    private static final Pattern pattern = Pattern.compile("([\\d]{1,3}[\\u002E]){3}[\\d]{1,3}:[\\d]{1,5}" +
            "[\\s]+[\\w||[-]]+[\\s]+.+");

    @Override
    public ProxyServer tryParse(String str)
    {
        
        Matcher matcher = pattern.matcher(str);
        if(!matcher.matches())
        {
            return null;
        }
        StringTokenizer tokenizer = new StringTokenizer(str, " :");
        String ip = tokenizer.nextToken();
        String port = tokenizer.nextToken();
        String location = tokenizer.nextToken();
        String ping = tokenizer.nextToken();
        ProxyServer server = new ProxyServer(ip, port);
        server.setLocation(location);
        server.setPing(ping);
        return server;
    }
}
